package com.qeado.admin.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qeado.admin.common.BaseConstData;
import com.qeado.admin.model.AdminAccount;
import com.qeado.admin.model.Menu;
import com.qeado.admin.service.AdminAccountManageService;

@Controller
public class LoginController {
	@Autowired
	private AdminAccountManageService adminAccountManageService;
	
	@RequestMapping("/login.ac")
	public ModelAndView login(HttpServletRequest request, String loginName, String passwd) {
		AdminAccount userInfo = adminAccountManageService.login(loginName, passwd);
		if(userInfo != null){
			request.getSession().setAttribute(BaseConstData.ADMIN_USER, userInfo);
			request.getSession().setAttribute(BaseConstData.USER_NAME, userInfo.getAccountName());
			request.getSession().setAttribute(BaseConstData.USER_ID, userInfo.getId());
	        return new ModelAndView("index/index");
		}
		return new ModelAndView("base/login_error","error","true");
    }

	@RequestMapping("/left.ac")
    public String getMenuList(HttpServletRequest request) {
        AdminAccount userInfo = (AdminAccount)request.getSession().getAttribute(BaseConstData.ADMIN_USER);
        List<Menu> menuList = adminAccountManageService.getMenuListByUser(userInfo);
        request.getSession().setAttribute(BaseConstData.MENU_LIST, menuList);
        return "index/left";
    }

	@RequestMapping("/main.ac")
    public String indexPage() {
        return "index/main";
    }
	
	@RequestMapping("/logout.ac")
    public String logout(HttpServletRequest request) {
		request.getSession().invalidate();
//        request.getSession().removeAttribute(BaseConstData.USER_NAME);
//        request.getSession().removeAttribute(BaseConstData.USER_ID);
//        request.getSession().removeAttribute(BaseConstData.ADMIN_USER);
//        request.getSession().removeAttribute(BaseConstData.MENU_LIST);
        return "redirect:index.htm";
    }
}
